package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/7/8 15:49
 */
public class Title529II {

    int n, m, num;

    public char[][] updateBoard(char[][] board, int[] click) {
        n = board.length;
        m = board[0].length;
        dfs(board, click[0], click[1]);
        return board;
    }

    private void dfs(char[][] board, int i, int j) {
        if (i < 0 || i >= n || j < 0 || j >= m || (board[i][j] >= '0' && board[i][j] <= '8') || board[i][j] == 'X' || board[i][j] == 'B') {
            return;
        }
        if (board[i][j] == 'M') {
            board[i][j] = 'X';
            return;
        }
        //计算四周的地雷个数  E
        int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
        int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1};
        num = 0;
        for (int k = 0; k < 8; k++) {
            getBombCount(board, i + dx[k], j + dy[k]);
        }
        if (num != 0) {
            board[i][j] = (char) (num + '0');
            return;
        }
        board[i][j] = 'B';
        for (int k = 0; k < 8; k++) {
            dfs(board, i + dx[k], j + dy[k]);
        }
    }

    private void getBombCount(char[][] board, int i, int j) {
        if (i < 0 || i >= n || j < 0 || j >= m) {
            return;
        }
        if (board[i][j] == 'M') {
            num++;
        }
    }


    public static void main(String[] args) {

    }
}
